package com.techwells.learningkites.controller;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.R.integer;
import android.app.Dialog;
import android.app.ExpandableListActivity;
import android.content.Intent;
import android.media.AudioManager;
import android.media.SoundPool;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CompoundButton;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.MediaController;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;
import android.widget.VideoView;

import com.lidroid.xutils.BitmapUtils;
import com.techwells.learningkites.R;
import com.techwells.learningkites.adapter.CourseExpandListAdapter;
import com.techwells.learningkites.adapter.DialogListAdapter;
import com.techwells.learningkites.adapter.SegmentViewPagerAdapter;
import com.techwells.learningkites.adapter.TestListAdapter;
import com.techwells.learningkites.adapter.WordListAdapter;
import com.techwells.learningkites.model.MainPageViewModel;
import com.techwells.learningkites.model.SegmentDetailViewModel;
import com.techwells.learningkites.networkaccess.KitesRequestUrl;
import com.techwells.learningkites.networkservice.KitesServiceMediator;
import com.techwells.learningkites.networkservice.model.Dialogue;
import com.techwells.learningkites.networkservice.model.Segment;
import com.techwells.learningkites.networkservice.model.Sentences;
import com.techwells.learningkites.networkservice.model.Test;
import com.techwells.learningkites.networkservice.model.Word;
import com.techwells.taco.mvvm.BaseActivity;
import com.techwells.taco.mvvm.Route;
import com.techwells.taco.networkservice.ServiceMediator;
import com.techwells.taco.tasktool.TaskToken;
import com.techwells.taco.utils.ToastUtils;

public class SegmentDetailActivity extends BaseActivity implements OnCheckedChangeListener, OnClickListener {
	
	private RadioGroup rgSegment;
	private RadioButton rbWord,rbDialog,rbTest;

	private ViewPager vpSegment;
	private List<View> vpViews;
	private View viewWord,viewDialog,viewTest;
	private SegmentViewPagerAdapter adapter;
	
//	private ExpandableListView elvWord;
//	private List<Word> listWord;
//	private WordExpandListAdapter wordListAdapter;
	private ListView lvWord;
	private WordListAdapter wordAdapter;
	
	private ListView lvDialog;
	private List<Dialogue> listDialog;
	private DialogListAdapter dialogListAdapter;
	
	private ListView lvTest;
	private List<Test> listTest;
	private TestListAdapter testListAdapter;
	
	private SegmentDetailViewModel presentViewModel;
	private Segment segment;
	private List<Word> wordList;
	private List<Sentences> sentenceList;
	private List<Test> testList;
	private VideoView video;
	private ImageView playPreview,btnPlay;
	private BitmapUtils bitmapUtils;
	
	private boolean isRefresh = true;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_segmet_detail);
		
		bitmapUtils = new BitmapUtils(this);
		initViews();
		initViewPager();
		initViewWord();
		initViewDialog();
		initViewTest();
		
	}
	

	private void initViewTest() {
		lvTest = (ListView) viewTest.findViewById(R.id.lv_test);
//		listTest = new ArrayList<Test>();
//		for (int i = 0; i < 8; i++) {
//			Test test = new Test();
//			listTest.add(test);
//		}
//		testListAdapter = new TestListAdapter(this, listTest);
//		lvTest.setAdapter(testListAdapter);
	}


	private void initViewDialog() {
		lvDialog =  (ListView) viewDialog.findViewById(R.id.lv_dialog);
//		listDialog = new ArrayList<Dialogue>();
//		for (int i = 0; i < 8; i++) {
//			Dialogue dialogue =  new Dialogue();
//			listDialog.add(dialogue);
//		}
//		dialogListAdapter = new DialogListAdapter(this, listDialog);
//		lvDialog.setAdapter(dialogListAdapter);
	}


	private void initViewWord() {
//		elvWord = (ExpandableListView) viewWord.findViewById(R.id.lv_words);
//		listWord = new ArrayList<Word>();
//		for (int i = 0; i < 10; i++) {
//			Word word = new Word();
//			listWord.add(word);
//        }
//		
//		wordListAdapter = new WordExpandListAdapter(this, listWord);
//		elvWord.setAdapter(wordListAdapter);
		lvWord = (ListView) viewWord.findViewById(R.id.lv_words);
	}


	private void initViewPager() {
		playPreview =  (ImageView) findViewById(R.id.play_preview);
		btnPlay = (ImageView) findViewById(R.id.btn_play);
		
		vpSegment = (ViewPager) findViewById(R.id.vp_segment);
		vpViews = new ArrayList<View>();
		viewWord = getLayoutInflater().inflate(R.layout.viewpager_words, null);
		viewDialog = getLayoutInflater().inflate(R.layout.viewpager_dialog, null);
		viewTest = getLayoutInflater().inflate(R.layout.viewpager_test, null);
		vpViews.add(viewWord);
		vpViews.add(viewDialog);
		vpViews.add(viewTest);
		adapter = new SegmentViewPagerAdapter(this, vpViews);
		vpSegment.setAdapter(adapter);
		vpSegment.setCurrentItem(0);
		vpSegment.setOffscreenPageLimit(3);
		vpSegment.setOnPageChangeListener(new OnPageChangeListener() {
			
			@Override
			public void onPageSelected(int arg0) {
				switch (arg0) {
				case 0:
					rbWord.setChecked(true);
					break;
				case 1:
					rbDialog.setChecked(true);
					break;
				case 2:
					rbTest.setChecked(true);
					break;

				default:
					break;
				}
			}
			
			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onPageScrollStateChanged(int arg0) {
				// TODO Auto-generated method stub
				
			}
		});
	}


	private void initViews() {
		rgSegment = (RadioGroup) findViewById(R.id.rg_segment);
		rbWord = (RadioButton) findViewById(R.id.tab_rb_words);
		rbDialog = (RadioButton) findViewById(R.id.tab_rb_dialog);
		rbTest = (RadioButton) findViewById(R.id.tab_rb_test);
		
		rbWord.setChecked(true);
		rgSegment.setOnCheckedChangeListener(this);
		
	}


	@Override
	public void requestFailedHandle(TaskToken token, int errorCode,
			String errorMsg) {
		// TODO Auto-generated method stub

	}

	@Override
	public void startProgressDialog(boolean cancle) {
		// TODO Auto-generated method stub

	}


	@Override
	public void onCheckedChanged(RadioGroup group, int checkedId) {
		switch (checkedId) {
		case R.id.tab_rb_words:
			vpSegment.setCurrentItem(0);
			break;
		case R.id.tab_rb_dialog:
			vpSegment.setCurrentItem(1);
			break;
		case R.id.tab_rb_test:
			vpSegment.setCurrentItem(2);
			break;

		default:
			break;
		}
		
	}

	
	@Override
	public void alreadyBindBaseViewModel() {
		// TODO Auto-generated method stub
		super.alreadyBindBaseViewModel();
		presentViewModel = (SegmentDetailViewModel) baseViewModel;
	}
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		
		if (isRefresh) {
			doRequestSegmentDetail();
			doRequestGetWords();
			doRequestGetSentences();
			doRequestGetTests();
		}

	}


	private void doRequestSegmentDetail() {
		int segmentId = getIntent().getIntExtra("segmentId", 0);
		HashMap<String, Object> argMap = new HashMap<String, Object>();	
		argMap.put("segmentId", segmentId);
		doTask(KitesServiceMediator.SERVICE_GET_VIDEO_SEGMENT_DETAILS,argMap);
		
	}


	private void doRequestGetTests() {
		int segmentId = getIntent().getIntExtra("segmentId", 0);
		HashMap<String, Object> argMap = new HashMap<String, Object>();	
		argMap.put("segmentId", segmentId);
		doTask(KitesServiceMediator.SERVICE_GET_VIDEO_SEGMENT_EXAMS,argMap);
		
	}


	private void doRequestGetSentences() {
		int segmentId = getIntent().getIntExtra("segmentId", 0);
		HashMap<String, Object> argMap = new HashMap<String, Object>();	
		argMap.put("segmentId", segmentId);
		doTask(KitesServiceMediator.SERVICE_GET_VIDEO_SEGMENT_SENTENCES,argMap);
		
	}


	private void doRequestGetWords() {
		int segmentId = getIntent().getIntExtra("segmentId", 0);
		HashMap<String, Object> argMap = new HashMap<String, Object>();	
		argMap.put("segmentId", segmentId);
		doTask(KitesServiceMediator.SERVICE_GET_VIDEO_SEGMENT_WORDS,argMap);
	}
	
	@Override
	public void refreshData(TaskToken token) {
		// TODO Auto-generated method stub
		super.refreshData(token);
		
		String method = token.method;
		if (method.equals(KitesServiceMediator.SERVICE_GET_VIDEO_SEGMENT_DETAILS)) {
			segment = ((SegmentDetailViewModel)baseViewModel).segment;
			bitmapUtils.display(playPreview, KitesRequestUrl.BASE_URL_IMG + segment.icon);
			btnPlay.setOnClickListener(this);
			
		}else if (method.equals(KitesServiceMediator.SERVICE_GET_VIDEO_SEGMENT_WORDS)) {
			wordList = ((SegmentDetailViewModel)baseViewModel).wordList;
			wordAdapter = new WordListAdapter(this, wordList);
			lvWord.setAdapter(wordAdapter);
			
		}else if (method.equals(KitesServiceMediator.SERVICE_GET_VIDEO_SEGMENT_SENTENCES)) {
			sentenceList = ((SegmentDetailViewModel)baseViewModel).sentenceList;
			dialogListAdapter = new DialogListAdapter(this, sentenceList);
			lvDialog.setAdapter(dialogListAdapter);
			
		}else if (method.equals(KitesServiceMediator.SERVICE_GET_VIDEO_SEGMENT_EXAMS)) {
			testList = ((SegmentDetailViewModel)baseViewModel).testList;
			testListAdapter = new TestListAdapter(this, testList);
			lvTest.setAdapter(testListAdapter);
			
		}else if (method.equals(KitesServiceMediator.SERVICE_DO_VIDEO_SENTENCE)) {
			int returnCode = ((SegmentDetailViewModel)baseViewModel).returnCode;
			if (returnCode == ServiceMediator.Service_Return_Success) {
				ToastUtils.show(this, "收藏成功");
			}
		}else if (method.equals(KitesServiceMediator.SERVICE_ADD_WORDS)) {
			int wordReturnCode = ((SegmentDetailViewModel)baseViewModel).wordReturnCode;
			if (wordReturnCode == ServiceMediator.Service_Return_Success) {
				ToastUtils.show(this, "收藏成功");
			}
		}else if (method.equals(KitesServiceMediator.SERVICE_GET_VIDEO_STREAM)) {
			String resourceCache = ((SegmentDetailViewModel)baseViewModel).resourceCache;
			Intent intent = new Intent(this, VideoViewPlayerActivity.class);;
			intent.putExtra("resource", resourceCache);
			Route.route().nextControllerWithIntent(this, VideoViewPlayerActivity.class.getName(), Route.WITHOUT_RESULTCODE,intent);
		}
	}


	private void doRequestGetVideoStream() {
		HashMap<String, Object> argMap = new HashMap<String, Object>();	
		argMap.put("url", KitesRequestUrl.BASE_URL_IMG + segment.resource);
		argMap.put("name", ""+segment.id);
		doTask(KitesServiceMediator.SERVICE_GET_VIDEO_STREAM,argMap);
		
	}


	
	
	@Override
	public void onClick(View v) {
		
		switch (v.getId()) {
		case R.id.btn_play:
//			Intent intent = new Intent(this, VideoViewPlayerActivity.class);;
			Intent intent = new Intent(this, VideoPlayerActivity.class);;
			intent.putExtra("resource", KitesRequestUrl.BASE_URL_VIDEO + segment.resource);
			intent.putExtra("name", ""+segment.id);
//			Route.route().nextControllerWithIntent(this, VideoViewPlayerActivity.class.getName(), Route.WITHOUT_RESULTCODE,intent);
			startActivity(intent);
//			doRequestGetVideoStream();
			break;

		default:
			break;
		}
		
	}
	
	
	
}
